package com.lijun.SpringBootJavaFX.utils;

import javafx.geometry.Rectangle2D;
import javafx.scene.Node;
import javafx.scene.layout.GridPane;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class WindowChangeHandler {

    //窗体是否为最大化
    boolean isMax;
    //窗体初始化坐标及大小
    double initX=100;
    double initY=50;
    double initWidth=1024;
    double initHeight=600;


    public WindowChangeHandler(boolean isMax, double initX, double initY, double initWidth, double initHeight) {
        this.isMax = isMax;
        this.initX = initX;
        this.initY = initY;
        this.initWidth = initWidth;
        this.initHeight = initHeight;
    }

    public <T extends Node> WindowChangeHandler setMinClicked(T minButton, Stage mainWindow){
        minButton.setOnMouseClicked((event)->{
            mainWindow.setIconified(true);
        });
        return this;
    }

    public <T extends Node> WindowChangeHandler setMaxClicked(T maxButton, Stage mainWindow){
        maxButton.setOnMouseClicked((event)->{
            changeWindowSizeAndButtonStyle(mainWindow, maxButton);
        });
        return this;
    }

    /**
     * 鼠标双击title
     * @param title
     * @param mainWindow
     */
    public <T extends Node> void setOnDoubleClick(GridPane title, Stage mainWindow,T maxButton) {
        title.setOnMouseClicked((event)->{
            if(event.getClickCount() == 2){
                changeWindowSizeAndButtonStyle(mainWindow, maxButton);
            }
        });
    }

    private <T extends Node>  void changeWindowSizeAndButtonStyle(Stage mainWindow, T maxButton){
        //获取当前显示器中心范围的大小
        Rectangle2D rectangle2d = Screen.getPrimary().getVisualBounds();
        isMax = !isMax;
        if (isMax) {
            // 最大化
            mainWindow.setX(rectangle2d.getMinX());
            mainWindow.setY(rectangle2d.getMinY());
            mainWindow.setWidth(rectangle2d.getWidth());
            mainWindow.setHeight(rectangle2d.getHeight());
            //改变button样式
            maxButton.setStyle("-fx-background-image: url('/img/winRestoreButton.png') ;");
        } else {
            // 缩放回原来的大小
            mainWindow.setX(initX);
            mainWindow.setY(initY);
            mainWindow.setWidth(initWidth);
            mainWindow.setHeight(initHeight);
            //改变button样式
            maxButton.setStyle("-fx-background-image: url('/img/winMaxButton.png') ;");
        }
    }
}
